9月 7 2017 Online Judge►LeetCode [LeetCode] 654 - Maximum Binary Tree 題意建造最大二元樹。 解法照題目描述模擬即可。 程式12345678910111213141516171819202122232425262728293031/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {number[]} nums * @return {TreeNode} */var constructMaximumBinaryTree = function(nums) { const build = (left, right) => { if ( left > right ) return null; let max = -1, maxIndex = left; for ( let i = left; i <= right ; i ++ ){ if ( nums[i] > max ){ max = nums[i]; maxIndex = i; } } const node = new TreeNode(nums[maxIndex]); node.left = build(left, maxIndex-1); node.right = build(maxIndex+1, right); return node; } return build(0, nums.length - 1);}; Newer [LeetCode] 647 - Palindromic Substrings Older [LeetCode] 670 - Maximum Swap